package com.xomily.devops.agent;

import cn.hutool.json.JSONObject;
import com.xomily.devops.agent.domain.*;

import com.xomily.devops.agent.util.CommonUtil;
import com.xomily.devops.agent.util.HttpUtil;
import com.xomily.devops.agent.util.OshiUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @version V1.0
 * @ClassName:ScheduledTask.java
 * @author: xomily (2327742507@qq.com)
 * @date: 2021年11月16日
 * @Description: ScheduledTask.java
 * @Copyright: 2021-2022 www.xomily.com. All rights reserved.
 */
@Component
public class XomilyDevopsAgentTask {

    @Autowired
    private HttpUtil restUtil;
    @Autowired
    private AgentConfig agentConfig;


    /**
     * 线程池
     */
    static ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 2, TimeUnit.MINUTES, new LinkedBlockingDeque<>());
    /**
     * 10秒后执行，每隔60秒执行, 单位：ms。
     */
    @Scheduled(initialDelay = 10 * 1000L, fixedRate = 60 * 1000)
    public void surveyTask() {
    	 JSONObject jsonObject = new JSONObject();
         Timestamp t = CommonUtil.getNow();
         DevAlarmLog alarmLog = new DevAlarmLog();
         alarmLog.setType("AgentRunError");
         alarmLog.setCreateTime(t);
         try {
             // 存储信息
             List<DevDiscLog> discLogList = OshiUtil.disc();
             // cpu信息
             DevCpuLog cpuLog = OshiUtil.cpu();
             // 内存信息
             DevMemLog memLog = OshiUtil.memory();

             // 网络流量信息
             List<DevNetLog> netLogList = OshiUtil.net();

             if (cpuLog != null) {
                 jsonObject.put("cpuLog", cpuLog);
             }
             if (memLog != null) {
                 jsonObject.put("memLog", memLog);
             }             
             if (netLogList != null) {
                 jsonObject.put("netLogList", netLogList);
             }            
             if (discLogList != null) {
                 jsonObject.put("discLogList", discLogList);
             }

         } catch (Exception e) {
             e.printStackTrace();
             alarmLog.setContent(e.toString());
             alarmLog.setCode( CommonUtil.stringToMD5(alarmLog.getType()+alarmLog.getContent()));
             if (!StringUtils.isEmpty(alarmLog.getContent())) {
                 jsonObject.put("alarmLog", alarmLog);
             }
         } finally {
             restUtil.post(agentConfig.getServerApi() + "/devops/agent/survey", jsonObject);
         }

    	 
    }    
    
    /**
     * 1秒后执行，每隔60分钟执行, 单位：ms。
     */
    @Scheduled(initialDelay = 1 * 1000L, fixedRate =60  * 1000)
    public void infoTask() {
    	 JSONObject jsonObject = new JSONObject();
         Timestamp t = CommonUtil.getNow();
         DevAlarmLog alarmLog = new DevAlarmLog();
         alarmLog.setType("AgentRunError");
         alarmLog.setCreateTime(t);
         try {
        	 
        	 DevHost host = OshiUtil.hostInfo();
             // 存储信息
             List<DevDisc> discList = OshiUtil.discInfo();
             

             // 网络信息
             List<DevNet> netList = OshiUtil.netInfo();

             if (host != null) {
                 jsonObject.put("host", host);
             }
             if (discList != null) {
                 jsonObject.put("discList", discList);
             }            
             if (netList != null) {
                 jsonObject.put("netList", netList);
             }

         } catch (Exception e) {
             e.printStackTrace();
             alarmLog.setContent(e.toString());
             alarmLog.setCode( CommonUtil.stringToMD5(alarmLog.getType()+alarmLog.getContent()));
             if (!StringUtils.isEmpty(alarmLog.getContent())) {
                 jsonObject.put("alarmLog", alarmLog);
             }
         } finally {
             restUtil.post(agentConfig.getServerApi() + "/devops/agent/info", jsonObject);
         }
    	 
    }  
    

}
